﻿using Nop.Core;
using Nop.Core.Domain.Customers;
using Nop.Services.Directory;
using Nop.Services.Helpers;
using System;
using System.Linq;
using System.Web.Mvc;

namespace SpotsHub.Gateway.Controllers
{
    public class CommonController : Controller
    {
        private readonly IDateTimeHelper _dateTimeHelper;
        private readonly IHolidayService _holidayService;
        private readonly IWorkContext _workContext;

        public CommonController(
            IDateTimeHelper dateTimeHelper,
            IHolidayService holidayService,
            IWorkContext workContext
            )
        {
            this._dateTimeHelper = dateTimeHelper;
            this._holidayService = holidayService;
            this._workContext = workContext;
        }

        public ActionResult RenderMessages()
        {
            return PartialView();
        }

        [ChildActionOnly]
        public ActionResult ShippingWarnings()
        {
            #region Holidays

            ViewBag.IsHoliday = false;
            ViewBag.IsSunday = false;
            ViewBag.IsSaturday = false;

            if (_workContext.CurrentCustomer.IsRegistered())
            {
                var dateTime = _dateTimeHelper.ConvertToUserTime(DateTime.UtcNow);

                var holiday = _holidayService.GetHolidayByDate(dateTime.Date);
                if (holiday != null)
                {
                    bool showWarning = false;
                    if (holiday.StateProvinces != null && holiday.StateProvinces.Count > 0)
                    {
                        var region = _workContext.CurrentCustomer.Region;
                        if (region != null)
                        {
                            var ditrict = region.Districts.FirstOrDefault();
                            if (ditrict != null && holiday.StateProvinces.Any(x => x.Id == ditrict.StateProvinceId))
                            {
                                showWarning = true;
                            }
                        }
                    }

                    if (showWarning)
                    {
                        ViewBag.IsHoliday = true;
                        ViewBag.IsSunday = false;
                        ViewBag.IsSaturday = false;   
                        ViewBag.Name = holiday.Name;
                        ViewBag.Description = holiday.Description;
                    }
                }
                else if (dateTime.Date.DayOfWeek == DayOfWeek.Sunday)
                {
                    ViewBag.IsHoliday = false;
                    ViewBag.IsSunday = true;
                    ViewBag.IsSaturday = false;
                }
                else if (dateTime.Date.DayOfWeek == DayOfWeek.Saturday)
                {
                    ViewBag.IsHoliday = false;
                    ViewBag.IsSunday = false;
                    ViewBag.IsSaturday = true;
                }
            }

            #endregion

            #region Warnings

            #endregion

            return View();
        }


        public ActionResult GetForm(string uID)
        {
            ViewBag.url = "https://safe.sandbox-gtpaysecure.net/hpf/1_1/?uID=" + uID;
            return View();
        }
    }
}
